#!/usr/bin/python
# Filename: ex_DSIntro_1_1.py

# sort string in personalized character order

# this program provide a solution of sorting,
# which is only sort the index, with no moving of real object.
# this will reduce the cost of moving data in memory.

import string

l = ["PAB", "5C", "PABC", "CXY", "CRSI", "7", "B899", "B9"]
order = string.uppercase + string.digits

def charcmp(x, y):
    i = order.index(x)
    j = order.index(y)
    if i == j:
        return (0)
    elif i < j:
        return (-1)
    elif i > j:
        return (1)

def strcmp(x, y):
    str1 = l[x]
    str2 = l[y]
    for i in range(min(len(str1), len(str2))):
        r = charcmp(str1[i], str2[i])
        if r != 0:
            return (r)
    return (0)


if __name__ == "__main__":
    l_index = range(0, len(l))
    print l_index
    for i in l_index:
        print l[i],
    l_index.sort(strcmp)
    print
    print l_index
    for i in l_index:
        print l[i],
